<template>
  <div class="contentShadow">
    <el-container>
      <el-header>
        <h1>学生信息表</h1>
      </el-header>
      <el-main>
        <el-form ref="form" :model="studentInformation" label-width="100px">
          <el-form-item label="学生编号" prop="userId">
            <el-input v-model="studentInformation.userId" disabled="disabled"></el-input>
          </el-form-item>
          <el-form-item label="学号" prop="studentId">
            <el-input v-model="studentInformation.studentId"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="studentInformation.name"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="studentInformation.gender">
              <el-radio label="1">男</el-radio>
              <el-radio label="0">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="级别" prop="major">
            <el-input v-model="studentInformation.grade"></el-input>
          </el-form-item>
          <el-form-item label="专业" prop="major">
            <el-select v-model="studentInformation.majorId" placeholder="学苑">
              <el-option v-for="item in majorList"
                         :key="item.majorId"
                         :label="`${item.majorName} (${item.majorCode})`"
                         :value="item.majorId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="学苑" prop="class">
            <el-select v-model="studentInformation.classId" placeholder="学苑">
              <el-option v-for="item in classList"
                         :key="item.classId"
                         :label="item.className"
                         :value="item.classId">
              </el-option>
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="导师" prop="mentorId">
                      <el-input v-model="studentInformation.mentorName"></el-input>
                    </el-form-item>-->
          <el-form-item label="导师">
            <el-select v-model="studentInformation.mentorId" placeholder="导师">
              <el-option v-for="item in mentorList"
                         :key="item.jobId"
                         :label="`${item.mentorName} (${item.jobId})`"
                         :value="item.mentorId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import {getStudentInformation, submitStudentInformation} from "@/api/admin";
import {acquireClassInformation, acquireMajorInformation, acquireMentorInformation} from "@/api/basic";

export default {
  name: "AddUser",
  data() {
    return {
      studentInformation: {
        userId: "",
        studentId: "",
        name: "",
        gender: null,
        grade: "",
        majorId: null,
        majorName: "",
        classId: null,
        className: "",
        mentorId: null,
        mentorName: ""
      },
      majorList: [
        {majorId: 0, majorCode: '000000', majorName: '未指定'},
        {majorId: 1, majorCode: '080902', majorName: '软件工程'}
      ],
      classList: [
        {classId: 0, className: '未指定'},
        {classId: 1, className: '求知一苑'},
        {classId: 2, className: '求知二苑'}
      ],
      mentorList: [{mentorId: '', jobId: '', mentorName: ''}],
    };
  },
  mounted() {
    this.studentInformation.userId = Number(window.location.search.substring(4));
    acquireMajorInformation().then(res => {
      this.majorList = res.data
    })
    acquireClassInformation().then(res => {
      this.classList = res.data
      this.classList.sort((a, b) => a.classOrder - b.classOrder);
    })
    acquireMentorInformation().then(res => {
      this.mentorList = res.data
    })

    if (this.studentInformation.userId !== 0) {
      getStudentInformation(this.studentInformation.userId).then((res) => {
        this.studentInformation = res.data;
        this.studentInformation.gender = this.studentInformation.gender.toString();
      });
    } else {
      console.log("以后处理新建用户的部分！")
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // TODO: send user data to backend
          console.log(this.studentInformation);
          submitStudentInformation(this.studentInformation).then(res => {
            if (res.code !== 200) {
              this.$message.error(res.msg);
            } else {
              this.$message.success("操作成功！");
              window.location.href = '/admin/student-list';
            }
          })
        } else {
          console.log("form validation failed");
        }
      });
    },
  },
};
</script>

<style>

</style>
